# -*- coding: utf-8 -*-
"""
Created on Fri May  5 17:31:06 2023

@author: LJM
"""
import pandas as pd

def feature_engineering(dfo,feature_map):                                                 #传入窄表特征数据 与解析映射表 返回 宽表的特征数据        
    feature_map.english = feature_map.english.str.strip()
    dic = feature_map.set_index('china').english.to_dict()                                #获取解析映射字典
    dfo.loc[dfo.index_content.str.lower()=='true','index_content'] = '1'                  #修改值
    dfo = dfo.set_index(['rating_id','index_name']).index_content.unstack().reset_index() #展开成宽表
    print(dfo.shape)
    dfo.columns = dfo.columns.str.lower()                                                 #大小写改掉
    dfo.columns = dfo.columns.str.replace('_','')
    dfo.rename(columns={'ratingid':'rating_id'},inplace=True)
    dfo.epcertywlxxgltx.fillna('Null',inplace=True)                                      #防止缺失值变成布尔掩码时出现错误
    print('开始进行解析')
    for key,value in dic.items():
        dfo.loc[dfo.epcertywlxxgltx.str.contains(key),value] = '1'
    print(dfo.shape)
    print('解析已经完成')
    return dfo 